"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，查看显示API调用时间/客户端信息/注册信息/展示auid
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，显示API调用时间
    2、进入交互模式，查看客户端信息
    3、进入交互模式，查看LUN/注册信息
    4、进入交互模式，查看该节点是否注册
    5、进入交互模式，展示auid
Expect      :
    1、查看成功
    2、查看成功
    3、查看成功
    4、查看成功
    5、查看成功
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，查看API调用时间   expect:查看成功-----'
        self.log.info(text)

        cmd = f'''dsscmd -i'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "ts\\n"
                     expect "dsscmded>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        result1 = self.node.sh(execute_cmd1).result()
        self.log.info(result1)
        self.assertEqual(8, result1.count('DSS'), '查看API调用时间失败')

        text = '-----step2:进入交互式模式，查看客户端信息   expect:查看成功-----'
        self.log.info(text)

        execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "lscli\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd2)
        res2 = self.node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('cli_pid', res2, '查看客户端信息失败')

        text = '-----step3:进入交互式模式，查看LUN/注册信息   expect:查看成功-----'
        self.log.info(text)

        param_list1 = ['inq -t lun', 'inq -t reg']
        for param in param_list1:
            execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd3)
            res3 = self.node.sh(execute_cmd3).result()
            self.log.info(res3)
            self.assertIn('Succeed to inquiry LUN information, or inquiry reservations', res3, '查看注册信息失败')

        text = '-----step4:进入交互模式，查询该节点是否注册  expect:查看成功-----'
        self.log.info(text)

        param_list2 = ['inq_reg -i 0', 'inq_reg -i 1', 'inq_reg -i 2',
                       'inq_reg -i 3', 'inq_reg -i 66']
        for param in param_list2:
            execute_cmd4 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd4)
            res4 = self.node.sh(execute_cmd4).result()
            self.log.info(res4)
            location = param_list2.index(param)
            if location < 3:
                self.assertIn(f'The node {location} is registered', res4, '查看节点是否注册失败')
            elif location == 3:
                self.assertIn(f'The node {location} is not registered', res4, '查看节点是否注册失败')
            else:
                self.assertIn('The value of inst_id should be in [0, 64)', res4, '查看节点是否注册失败')

        text = '-----step5:进入交互模式，显示auid  expect:成功-----'
        self.log.info(text)

        param_list3 = ['auid -a 0', 'auid -a 1', 'auid -a 2',
                       'auid -a 123456', 'auid -a 999999']
        for param in param_list3:
            execute_cmd5 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd5)
            res5 = self.node.sh(execute_cmd5).result()
            self.log.info(res5)
            location = param_list3.index(param)
            if location < 3:
                self.assertIn('auid', res5, '查看auid失败')
            else:
                self.assertIn('block_id is invalid', res5, '查看auid成功')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
